import { Toast } from 'antd-mobile'
import _ from 'lodash'
import { createAction, NavigationActions, URL } from '../utils'
import { post, get, postFile } from '../utils/fetch'

export default {
  namespace: 'upload',
  state: {
    loading: true,
    firstStep: {
      travelType: null,
      area: null,
      startTime: null,
      endTime: null,
      attraction: null,
      files: null,
      comments: ''
    },
    uploading: false
  },
  reducers: {
    updateState(state, { payload }) {
      return { ...state, ...payload }
    },
    firstStepState(state, { payload }) {
      const {
        travelType,
        startTime,
        endTime,
        attraction,
        area
      } = payload.payload
      return {
        ...state,
        firstStep: {
          travelType: _.isNil(travelType) ? state.firstStep.travelType : travelType,
          startTime: _.isNil(startTime) ? state.firstStep.startTime : startTime,
          endTime: _.isNil(endTime) ? state.firstStep.endTime : endTime,
          attraction: _.isNil(attraction) ? state.firstStep.attraction : attraction,
          area: _.isNil(area) ? state.firstStep.area : area
        }
      }
    },
    saveUpload(state, { payload }) {
      const { data, comments } = payload
      return {
        ...state,
        files: data,
        comments
      }
    },
    updataLove(state, { payload }) {
      return {
        ...state,
        uploading: true
      }
    },
    initState(state, { payload }) {
      return {
        ...state,
        firstStep: {
          travelType: null,
          area: null,
          startTime: null,
          endTime: null,
          attraction: null,
          files: null,
          comments: ''
        }
      }
    }
  },
  effects: {
    * loadUpload(action, { call, put }) {
      yield put(createAction('updateState')({ loading: false }))
    },
    * firstStep({ payload }, { call, put }) {
      yield put(createAction('firstStepState')({ payload }))
    },
    * submit({ payload }, { call, put, select }) {
      try {
        const { files, comments } = payload
        const { file } = yield select(state => state.upload)
        const data = yield call(() => postFile(`${URL}2/files/${files.fileName}`, files))
        if (data) {
          yield put(createAction('saveUpload')({ data, comments }))
          console.log('test upload data', data)
          yield put({ type: 'submitAll' })
        } else {
          Toast.fail('图片上传失败', 1)
        }
      } catch (e) {
        console.log(e)
      }
    },
    * submitAll({ payload }, { call, select, put }) {
      try {
        const {
          firstStep, files, comments
        } = yield select(state => state.upload)
        const { user } = yield select(state => state.login)
        const {
          travelType,
          area,
          startTime,
          endTime,
          attraction
        } = firstStep
        if (_.isNil(user)) {
          Toast.fail('您尚未登录！！！', 1)
        } else {
          console.log('test user data', user)
          const travels = {
            type: travelType,
            startTime,
            endTime,
            attraction,
            area,
            file: {//关联上传的图片
              __type: 'File',
              group: 'group1',
              filename: files.filename,
              url: files.url
            },
            user: {
              __type: 'Pointer',
              className: '_User',
              objectId: user.objectId
            },
            love: [],
            log: comments,
            comments: '',
            collection: []
          }
          const data = yield call(() => post(`${URL}1/classes/Travels`, travels))
          if (data) {
            if (_.has(data, 'code')) {
              Toast.fail(data.error)
            } else {
              yield put({ type: 'updataLove' })
              yield put({ type: 'initState' })
            }
          } else {
            Toast.fail('信息上传失败', 1)
          }
        }
      } catch (e) {
        console.log(e)
      }
    }
  },
  subscriptions: {
    setup({ dispatch }) {
      dispatch({ type: 'loadUpload' })
    }
  }
}
